# Maintainer: Martell Malone <MartellMalone@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=clang
pkgbase=clang-svn
pkgname=("llvm-svn"
         #"lldb-svn"
         #"compiler-rt-svn"
         #"libc++abi-svn"
         #"libc++-svn"
         "lld-svn"
         "clang-svn"
         #"clang-analyzer-svn"
         #"clang-tools-extra-svn"
         )
pkgver=60209.20d4cdc
pkgrel=1
pkgdesc="C language family frontend for LLVM"
arch=('i686' 'x86_64')
url="https://llvm.org/"
license=("custom:University of Illinois/NCSA Open Source License")
makedepends=("gcc"
             "libffi-devel"
             "libxml2-devel"
             "pkg-config"
             "python2"
             "tar"
             "subversion"
             "git"
             "cmake")
depends=("gcc" "libffi" "libxml2")
options=('staticlibs') # 'debug' '!strip')
source=("llvm"::"git+https://llvm.org/git/llvm.git"
        "lld"::"git+https://llvm.org/git/lld.git"
        "clang"::"git+https://llvm.org/git/clang.git"
        #"clang-tools-extra"::"git+https://llvm.org/git/clang-tools-extra.git"
        #"compiler-rt"::"git+https://llvm.org/git/compiler-rt.git"
        #"lldb"::"git+https://llvm.org/git/lldb.git"
        #"testsuite"::"git+https://llvm.org/git/test-suite.git"
        #"libcxxabi"::"git+https://llvm.org/git/libcxxabi.git"
        #"libcxx"::"git+https://llvm.org/git/libcxx.git"
        "0001-cmake-hack-for-msys2.patch"
        "0011-Driver-Better-detection-of-mingw-gcc.patch"
        "0012-Driver-Pass-compiler-rt-more-like-libgcc.patch"
        "0021-add-support-for-gnu-in-coff.patch")

sha256sums=('SKIP'
            'SKIP'
            'SKIP'
            '0ef3d97bd3650416e75844f732edd9530e060071c98724acc4bfe86eb16f7cb2'
            '1dc131f51c636b0f73dff98f12a1e91046bb9efadff1ea18d1da230d7a539242'
            '72516a9e26d588bf8254e46e2cc6c3355d07cce37405af46b59348a78a2d1ad2'
            'e47e9b6f35c86ab3674f8ec01e7e2bd755d4daf7900a90be9fe7746328d82039')

pkgver_internal() {
  if [ ! -d "$1" ]; then
    plain "No such directory $1"
    exit 1
  fi
  pushd "$1" > /dev/null 2>&1
  printf "%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
  popd > /dev/null 2>&1
}

pkgver() {
  pkgver_internal "${srcdir}"/clang
}

prepare() {
  cd "${srcdir}"/llvm
  git am "${srcdir}"/0001-cmake-hack-for-msys2.patch

  # Fix docs installation directory
  sed -i 's:$(PROJ_prefix)/docs/llvm:$(PROJ_prefix)/share/doc/llvm:' \
    Makefile.config.in

  # Fix definition of LLVM_CMAKE_DIR in LLVMConfig.cmake
  sed -i '/@LLVM_CONFIG_CMAKE_DIR@/s:$(PROJ_cmake):$(PROJ_prefix)/share/llvm/cmake:' \
    cmake/modules/Makefile

  cd "${srcdir}"/clang
  git am "${srcdir}"/0011-Driver-Better-detection-of-mingw-gcc.patch
  git am "${srcdir}"/0012-Driver-Pass-compiler-rt-more-like-libgcc.patch

  cd "${srcdir}"/lld
  git am "${srcdir}"/0021-add-support-for-gnu-in-coff.patch

  cd "${srcdir}"/llvm

  # At the present, clang must reside inside the LLVM source code tree to build
  # See https://llvm.org/bugs/show_bug.cgi?id=4840
  mv "$srcdir/clang" tools/clang

# mv "$srcdir/clang-tools-extra" tools/clang/tools/extra
  mv "$srcdir/lld" tools/lld
# mv "$srcdir/lldb" tools/lldb
# mv "$srcdir/compiler-rt" projects/compiler-rt
# mv "$srcdir/libcxxabi" projects/libcxxabi
# mv "$srcdir/libcxx" projects/libcxx
# mv "$srcdir/testsuite" projects/test-suite
  mkdir build-${CARCH}
}

build() {
  cd "$srcdir"

  [[ -d build-${CARCH} ]] && rm -rf build-${CARCH}
  mkdir build-${CARCH} && cd build-${CARCH}

  local -a extra_config

  if check_option "debug" "y"; then
    extra_config+=(-DCMAKE_BUILD_TYPE=Debug)
  else
    extra_config+=(-DCMAKE_BUILD_TYPE=Release)
  fi

  # Include location of libffi headers in CPPFLAGS
  FFI_INCLUDE_DIR="$(pkg-config --cflags libffi)"
  FFI_INCLUDE_DIR=$(echo $FFI_INCLUDE_DIR | sed 's|-I||g')

  cmake \
    -G"Unix Makefiles" \
    -DCMAKE_MAKE_PROGRAM="/usr/bin/make.exe" \
    -DCMAKE_C_COMPILER="/usr/bin/gcc.exe" \
    -DCMAKE_CXX_COMPILER="/usr/bin/g++.exe" \
    -DFFI_INCLUDE_DIR="${FFI_INCLUDE_DIR}" \
    -DCMAKE_C_FLAGS="${CFLAGS}" \
    -DCMAKE_CXX_FLAGS="${CXXFLAGS} ${CPPFLAGS}" \
    -DCMAKE_INSTALL_PREFIX="/usr" \
    -DLLVM_TARGETS_TO_BUILD="ARM;CppBackend;X86;PowerPC" \
    -DLLVM_ENABLE_ASSERTIONS=OFF \
    -DLLVM_ENABLE_THREADS=0 \
    -DPYTHON_EXECUTABLE=/usr/bin/python2 \
    -DLLVM_ENABLE_FFI=ON \
    -DLLVM_ENABLE_SPHINX=OFF \
    "${extra_config[@]}" \
    ../llvm

#    -DLLVM_ENABLE_SPHINX=ON \
#    -DCMAKE_CXX_FLAGS="-D_GNU_SOURCE -D_LIBCPP_HAS_NO_CONSTEXPR" \
#    -DLIBCXX_ENABLE_SHARED=OFF \
#    -DLIBCXXABI_ENABLE_SHARED=OFF \

  make

  # Disable automatic installation of components that go into subpackages
  sed -i '/\(clang\|lld\|lldb\)\/cmake_install.cmake/d' tools/cmake_install.cmake
  sed -i '/extra\/cmake_install.cmake/d' tools/clang/tools/cmake_install.cmake
  sed -i '/compiler-rt\/cmake_install.cmake/d' projects/cmake_install.cmake
}

#check() {
#  cd "${srcdir}"/build-${CARCH}
#  # Remove || true once testcase doesn't cause failures.
#  make check || true
#}

package_llvm-svn() {
  pkgdesc="Low Level Virtual Machine"
  #depends=("gcc") # "compiler-rt"
  provides=("llvm")
  conflicts=("llvm")

  pkgver=$(pkgver_internal "${srcdir}"/llvm)

  cd "${srcdir}"/llvm

  make -C ../build-${CARCH} DESTDIR="$pkgdir" install

  install -Dm644 LICENSE.TXT "$pkgdir/usr/share/licenses/llvm/LICENSE"

  # Install CMake stuff
  install -d "${pkgdir}"/usr/share/llvm/cmake/{modules,platforms}
  install -Dm644 "${srcdir}"/llvm/cmake/modules/*.cmake "${pkgdir}"/usr/share/llvm/cmake/modules/
  install -Dm644 "${srcdir}"/llvm/cmake/platforms/*.cmake "${pkgdir}"/usr/share/llvm/cmake/platforms/
}

 package_compiler-rt() {
  pkgdesc="Runtime libraries for Clang and LLVM"
  url="https://compiler-rt.llvm.org/"
  depends=("llvm-svn")
  provides=("compiler-rt")

  conflicts=("compiler-rt")
  pkgver=$(pkgver_internal "${srcdir}"/llvm/projects/compiler-rt)


  cd "$srcdir/llvm"
  make -C ../build-${CARCH}/projects/compiler-rt DESTDIR="$pkgdir" install
 }

package_libcxxabi() {
  pkgdesc="C++ Standard Library Support"
  url="https://libcxxabi.llvm.org/"
  provides=("libc++abi")
  conflicts=("libc++abi")
  pkgver=$(pkgver_internal "${srcdir}"/llvm/projects/libcxxabi)

  cd "$srcdir/llvm"
  
  make -C ../build-${CARCH}/projects/libcxxabi -j1 DESTDIR="$pkgdir" install
}

package_libcxx() {
  pkgdesc="C++ Standard Library"
  url="https://libcxx.llvm.org/"
  provides=("libc++")
  conflicts=("libc++")
  pkgver=$(pkgver_internal "${srcdir}"/llvm/projects/libcxx)

  cd "$srcdir/llvm"
   make -C ../build-${CARCH}/projects/libcxx -j1 DESTDIR="$pkgdir" install
}

package_lldb-svn() {
  pkgdesc="Next generation, high-performance debugger"
  url="https://lldb.llvm.org/"
  depends=('libedit' 'libxml2' 'python2')

  cd "$srcdir/llvm-$pkgver.src"

  make -C ../build-${CARCH}/tools/lldb DESTDIR="$pkgdir" install

  # Compile Python scripts
  python2 -m compileall "$pkgdir/usr/lib/python2.7/site-packages/lldb"
  python2 -O -m compileall "$pkgdir/usr/lib/python2.7/site-packages/lldb"

  install -Dm644 tools/lldb/LICENSE.TXT "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}


package_lld-svn() {
  pkgdesc="Linker tools for LLVM"
  url="https://lld.llvm.org/"
  depends=("llvm-svn")
  provides=("lld")
  conflicts=("lld")
  pkgver=$(pkgver_internal "${srcdir}"/llvm/tools/lld)

  cd "${srcdir}/llvm"
  make -C ../build-${CARCH}/tools/lld -j1 DESTDIR="$pkgdir" install
}

package_clang() {
  pkgdesc="C language family frontend for LLVM"
  url="https://clang.llvm.org/"
  depends=("llvm-svn")
  provides=("clang")
  conflicts=("clang")
  pkgver=$(pkgver_internal "${srcdir}"/llvm/tools/clang)

  cd "$srcdir/llvm"
  make -C ../build-${CARCH}/tools/clang DESTDIR="$pkgdir" install

  mkdir -p "${pkgdir}/opt/bin"
  echo "#!/usr/bin/env bash" > ${pkgdir}/opt/bin/i686-w64-mingw32-clang
  echo '/usr/bin/clang -target i686-windows-gnu "$@"' >> ${pkgdir}/opt/bin/i686-w64-mingw32-clang
  echo "#!/usr/bin/env bash" > ${pkgdir}/opt/bin/i686-w64-mingw32-clang++
  echo '/usr/bin/clang++ -target i686-windows-gnu "$@"' >> ${pkgdir}/opt/bin/i686-w64-mingw32-clang++
  echo "#!/usr/bin/env bash" > ${pkgdir}/opt/bin/x86_64-w64-mingw32-clang
  echo '/usr/bin/clang -target x86_64-windows-gnu "$@"' >> ${pkgdir}/opt/bin/x86_64-w64-mingw32-clang
  echo "#!/usr/bin/env bash" > ${pkgdir}/opt/bin/x86_64-w64-mingw32-clang++
  echo '/usr/bin/clang++ -target x86_64-windows-gnu "$@"' >> ${pkgdir}/opt/bin/x86_64-w64-mingw32-clang++
  echo "#!/usr/bin/env bash" > ${pkgdir}/opt/bin/armv7-w64-mingw32-clang
  echo '/usr/bin/clang -target armv7-windows-gnu "$@"' >> ${pkgdir}/opt/bin/armv7-w64-mingw32-clang
  echo "#!/usr/bin/env bash" > ${pkgdir}/opt/bin/armv7-w64-mingw32-clang++
  echo '/usr/bin/clang++ -target armv7-windows-gnu "$@"' >> ${pkgdir}/opt/bin/armv7-w64-mingw32-clang++

}

package_clang-svn() {
  package_clang
}

package_clang-analyzer-svn() {
  pkgdesc="A source code analysis framework"
  url="https://clang-analyzer.llvm.org/"
  depends=("clang-svn" "python2")
  provides=("clang-analyzer")
  conflicts=("clang-analyzer")
  pkgver=$(pkgver_internal "${srcdir}"/llvm/tools/clang)

  cd "${srcdir}"/llvm/tools/clang

  install -d "${pkgdir}"/usr/{bin,lib/clang-analyzer}
  for _tool in scan-{build,view}; do
    cp -r tools/$_tool "${pkgdir}"/usr/lib/clang-analyzer/
    ln -s "${pkgdir}"/usr/lib/clang-analyzer/$_tool/$_tool "${pkgdir}"/usr/bin/
  done

  # scan-build looks for clang within the same directory
  install -d "${pkgdir}"/usr/lib/clang-analyzer/scan-build/
  ln -s "${srcdir}"/build-${CARCH}/bin/clang.exe "${pkgdir}"/usr/lib/clang-analyzer/scan-build/

  # Relocate man page
  install -d "${pkgdir}"/usr/share/man/man1
  mv "${pkgdir}"/usr/lib/clang-analyzer/scan-build/scan-build.1 \
     "${pkgdir}"/usr/share/man/man1/

  # Use Python 2
  #sed -i \
  #  -e 's|env python$|&2|' \
  #  -e 's|/usr/bin/python$|&2|' \
  #  "${pkgdir}"/usr/lib/clang-analyzer/scan-view/scan-view \
  #  "${pkgdir}"/usr/lib/clang-analyzer/scan-build/set-xcode-analyzer

  # Compile Python scripts
  python2 -m compileall "${pkgdir}"/usr/lib/clang-analyzer
  python2 -O -m compileall "${pkgdir}"/usr/lib/clang-analyzer
  install -Dm644 LICENSE.TXT "${pkgdir}"/usr/share/licenses/clang-analyzer/LICENSE
}

package_clang-tools-extra-svn() {
  pkgdesc="Extra tools built using Clang's tooling APIs"
  url="https://clang.llvm.org/"
  depends=("clang-svn")
  provides=("clang-tools-extra")
  conflicts=("clang-tools-extra")
  pkgver=$(pkgver_internal "${srcdir}"/llvm/tools/clang/tools/extra)

  cd "$srcdir"/llvm
  make -C ../build-${CARCH}/tools/clang/tools/extra -j1 DESTDIR="$pkgdir" install
}

# vim:set ts=2 sw=2 et:
